package Springboot.service.impl;

import Springboot.Utils.JwtHelper;
import Springboot.Utils.Result;
import Springboot.Utils.ResultCodeEnum;
import Springboot.domain.NewsUser;
import Springboot.domain.news.pojo;
import Springboot.mapper.NewsUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import Springboot.domain.NewsHeadline;
import Springboot.service.NewsHeadlineService;
import Springboot.mapper.NewsHeadlineMapper;
import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author mazhenxi
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-09-03 17:51:19
*/
@Service
public class NewsHeadlineServiceImpl extends ServiceImpl<NewsHeadlineMapper, NewsHeadline>
    implements NewsHeadlineService{
    @Autowired
    NewsHeadlineMapper newsHeadlineMapper;
    @Autowired
    JwtHelper jwtHelper;
    @Override
    public Result findNewsPage(pojo pageInfo) {
//        LambdaQueryWrapper<pojo> lambdaQueryWrapper=new LambdaQueryWrapper();
//        LambdaQueryWrapper<pojo> wrapper = lambdaQueryWrapper.like((page.getKeyWords() != null), pojo::getKeyWords, page.getKeyWords())
//                .eq((page.getType() != null), pojo::getType, page.getType());
        Page<NewsHeadline> headlinePage = new Page<>(pageInfo.getPageNum(),pageInfo.getPageSize());
        newsHeadlineMapper.selectByPage(headlinePage,pageInfo);
        Map<String, Object> map=new HashMap<>();
        map.put("pageData",headlinePage.getRecords());
        //System.out.println(headlinePage.getRecords());
        map.put("pageNum",headlinePage.getCurrent());
        map.put("pageSize",headlinePage.getSize());
        map.put("totalPage",headlinePage.getPages());
        map.put("totalSize",headlinePage.getTotal());
        Map<String,Object> totalMap=new HashMap<>();
        totalMap.put("pageInfo",map);
        return Result.ok(totalMap);
    }

    @Override
    public Result showHeadlineDetail(Integer id) {
        System.out.println(id);
        Map<String, Object> map = newsHeadlineMapper.selectDetailMap(id);
        System.out.println(map);
        NewsHeadline newsHeadline = new NewsHeadline();
        newsHeadline.setPageViews((Integer) map.get("pageViews")+1);
        newsHeadline.setVersion((Integer) map.get("version"));
        newsHeadline.setHid((Integer) map.get("hid"));
        System.out.println(newsHeadline);
        newsHeadlineMapper.updateById(newsHeadline);
        Map<String,Object> resultmap=new HashMap<>();
        resultmap.put("headline",map);
        return Result.ok(resultmap);
    }

    @Override
    public Result publish(NewsHeadline newsHeadline,String token) {
        Integer userId = jwtHelper.getUserId(token).intValue();
        newsHeadline.setPublisher(userId);
        newsHeadline.setUpdateTime(new Date());
        newsHeadline.setCreateTime(new Date());
        newsHeadline.setPageViews(0);
        int insert = newsHeadlineMapper.insert(newsHeadline);
        if(insert>0) {
            return Result.ok(null);
        }
        return Result.build(null, ResultCodeEnum.NOTLOGIN);
    }

    @Override
    public Result newsHeadlineService(Integer hid) {
        NewsHeadline newsHeadline = newsHeadlineMapper.selectById(hid);
        Map<String,Object> map=new HashMap<>();
        map.put("headline",newsHeadline);
        return Result.ok(map);
    }

    @Override
    public Result update(NewsHeadline newsHeadline) {
        newsHeadline.setUpdateTime(new Date());
        Integer verson = newsHeadlineMapper.selectById(newsHeadline).getVersion();
        newsHeadline.setVersion(verson);
        newsHeadlineMapper.updateById(newsHeadline);
        return Result.ok(null);
    }

    @Override
    public Result removeByHid(Integer hid) {
        newsHeadlineMapper.deleteById(hid);

        return Result.ok(null);
    }
}




